package com.flyco.modules.sms.service.impl;

import com.flyco.modules.sms.entity.SmsAddbuyInfo;
import com.flyco.modules.sms.entity.SmsAddbuyCondition;
import com.flyco.modules.sms.entity.SmsAddbuyBenefit;
import com.flyco.modules.sms.mapper.SmsAddbuyConditionMapper;
import com.flyco.modules.sms.mapper.SmsAddbuyBenefitMapper;
import com.flyco.modules.sms.mapper.SmsAddbuyInfoMapper;
import com.flyco.modules.sms.service.ISmsAddbuyInfoService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 加价购
 * @Author: flyco
 * @Date:   2020-04-08
 * @Version: V1.0
 */
@Service
public class SmsAddbuyInfoServiceImpl extends ServiceImpl<SmsAddbuyInfoMapper, SmsAddbuyInfo> implements ISmsAddbuyInfoService {

	@Autowired
	private SmsAddbuyInfoMapper smsAddbuyInfoMapper;
	@Autowired
	private SmsAddbuyConditionMapper smsAddbuyConditionMapper;
	@Autowired
	private SmsAddbuyBenefitMapper smsAddbuyBenefitMapper;
	
	@Override
	@Transactional
	public void saveMain(SmsAddbuyInfo smsAddbuyInfo, List<SmsAddbuyCondition> smsAddbuyConditionList,List<SmsAddbuyBenefit> smsAddbuyBenefitList) {
		smsAddbuyInfoMapper.insert(smsAddbuyInfo);
		for(SmsAddbuyCondition entity:smsAddbuyConditionList) {
			//外键设置
			entity.setInfoId(smsAddbuyInfo.getId());
			smsAddbuyConditionMapper.insert(entity);
		}
		for(SmsAddbuyBenefit entity:smsAddbuyBenefitList) {
			//外键设置
			entity.setInfoId(smsAddbuyInfo.getId());
			smsAddbuyBenefitMapper.insert(entity);
		}
	}

	@Override
	@Transactional
	public void updateMain(SmsAddbuyInfo smsAddbuyInfo,List<SmsAddbuyCondition> smsAddbuyConditionList,List<SmsAddbuyBenefit> smsAddbuyBenefitList) {
		smsAddbuyInfoMapper.updateById(smsAddbuyInfo);
		
		//1.先删除子表数据
		smsAddbuyConditionMapper.deleteByMainId(smsAddbuyInfo.getId());
		smsAddbuyBenefitMapper.deleteByMainId(smsAddbuyInfo.getId());
		
		//2.子表数据重新插入
		for(SmsAddbuyCondition entity:smsAddbuyConditionList) {
			//外键设置
			entity.setInfoId(smsAddbuyInfo.getId());
			smsAddbuyConditionMapper.insert(entity);
		}
		for(SmsAddbuyBenefit entity:smsAddbuyBenefitList) {
			//外键设置
			entity.setInfoId(smsAddbuyInfo.getId());
			smsAddbuyBenefitMapper.insert(entity);
		}
	}

	@Override
	@Transactional
	public void delMain(Long id) {
		smsAddbuyConditionMapper.deleteByMainId(id);
		smsAddbuyBenefitMapper.deleteByMainId(id);
		smsAddbuyInfoMapper.deleteById(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			smsAddbuyConditionMapper.deleteByMainId(Long.valueOf( id.toString() ));
			smsAddbuyBenefitMapper.deleteByMainId(Long.valueOf( id.toString() ));
			smsAddbuyInfoMapper.deleteById(id);
		}
	}
	
}
